/*
 * @Author: DUST
 * @Date: 2023-06-17 16:17:55
 * @LastEditors: DUST
 * @LastEditTime: 2023-06-17 20:03:38
 * @FilePath: \koa-node-ts\app\service\UtilsService.ts
 * @Description:
 * undefined
 */

import requestApi from "../../utils/request";

enum NewsEnum {
  "BAIDU" = "baidu",
  "WEIBO" = "weibo",
}
interface BaiduData {
  url: string;
  word: string;
}
interface WeiboData {
  data: {
    band_list: Array<{ word: string; rank: number }>;
  };
  code: number;
}
interface NewsList {
  baidu?: Array<BaiduData>;
  weibo?: Array<{ word: string; rank: number; url: string }>;
}

class UtilsService {
  getHotNews(newTypeList: Array<string>) {
    return new Promise(async (resolve, reject) => {
      const data: NewsList = {};
      // 百度热搜
      if (newTypeList.includes(NewsEnum.BAIDU)) {
        const res = await requestApi<{ data: Array<BaiduData> }>(
          "get",
          `https://api.1314.cool/getbaiduhot/`
        );
        data[NewsEnum.BAIDU] = res.data.map((val: BaiduData, i: number) => ({
          ...val,
          index: i + 1,
        }));
      }
      if (newTypeList.includes(NewsEnum.WEIBO)) {
        const res = await requestApi<WeiboData>(
          "get",
          "https://weibo.com/ajax/statuses/hot_band"
        );
        data[NewsEnum.WEIBO] = res.data.band_list.map((val, i: number) => ({
          ...val,
          index: i + 1,
          url: `https://s.weibo.com/weibo?q=${val.word}&Refer=top`,
        }));
      }

      resolve(data);
    });
  }
}

export default new UtilsService();
